home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / 4cmp22s.zip / INTERUPT.4TH < prev    next >
Text File  |  1994-10-30  |  4KB  |  126 lines

  1. \ Demonstration of INTS and EXCEPTIO (Catch/Throw) packages
  2. \ Contents of this file placed in Public Domain by the author, Tom Almy
  3.  
  4. \ Control-Break sets a flag (BREAK-SET) the demo does a Ctrl-Break THROW
  5. \ from the KEYS? word, but this could be placed in the EMIT definition
  6. \ and others as well.
  7.  
  8. \ Control-C does a Ctrl-C THROW.
  9. \ Divide by zero does a 0Divide THROW.
  10.  
  11.  
  12. \ STOP EXECUTION BY TYPING ANY KEY BUT "CONTROL-C" OR "CONTROL-
  13. \   BREAK"
  14.  
  15.  
  16. 256 MSDOS
  17. HEX
  18.  
  19. INCLUDE INTS
  20. INCLUDE EXCEPTIO
  21.  
  22. 23 CONSTANT CC-INT  ( Control-C software interrupt number from DOS)
  23. 1B CONSTANT CB-INT  ( Control-Break software interrupt from BIOS)
  24. 0  CONSTANT /0-INT  ( Zero Divide interrupt )
  25.  
  26. \ Throw values:
  27. DECIMAL
  28. -28 CONSTANT Ctrl-C      ( User interrupt )
  29.  28 CONSTANT Ctrl-Break  ( Not defined by standard )
  30. -10 CONSTANT 0Divide
  31. HEX
  32.  
  33.  
  34. \ CONTROL-C HANDLER
  35.  
  36. L: CC-ENTRY ( actual interrupt handler )
  37.   DECIMAL Ctrl-C HEX # AX MOV   AX PUSH 
  38.   CALL' THROW  \ Never returns
  39.  
  40.  
  41.  
  42. \ I don;t know how to throw from this
  43. \ CONTROL-BREAK HANDLER
  44. 20 ALLOT  HERE CONSTANT PARAMSTACK    \ our new stacks
  45. 20 ALLOT  HERE CONSTANT RETSTACK
  46. 2VARIABLE SS-SAVE            \ Stack segment save area
  47.  
  48. VARIABLE BREAK-SET
  49.  
  50. 0 0 IN/OUT
  51. : CB-HANDLER  BREAK-SET ON  ;
  52.  
  53.  
  54. L: CB-ENTRY ( actual interrupt handler )
  55.   ( save registers )
  56.     AX PUSH  DS PUSHSEG  AX CS <SEG  AX DS >SEG    \ save AX, DS, set DS
  57.     SS-SAVE [] SS <SEG  SP SS-SAVE CELL+ [] MOV    \ save SS SP
  58.     AX SS >SEG PARAMSTACK # SP MOV            \ set SS,SP
  59.     BX PUSH  CX PUSH  DX PUSH  SI PUSH  DI PUSH    \ Save remaining
  60.     BP PUSH ES PUSHSEG 
  61.     RETSTACK # BP MOV                \ set BP
  62.     CALL' CB-HANDLER  ( high level interrupt handler )
  63.     ES POPSEG BP POP DI POP SI POP DX POP CX POP BX POP \ restore registers
  64.     SS-SAVE [] SS >SEG SS-SAVE CELL+ [] SP MOV DS POPSEG  AX POP
  65.     IRET FORTH
  66.  
  67. \ Because this is a BIOS interupt, rather than an MS-DOS interupt,
  68. \ as CC-INT was, we have to set our data segment register, and 
  69. \ use new stacks with minimum impact on the DOS stack
  70.  
  71.  
  72. \  ZERO-DIVIDE TRAP HANDLER
  73.  
  74. L: /0-ENTRY  
  75.     0Divide # AX MOV AX PUSH 
  76.         CALL' THROW
  77.  
  78.  
  79.  
  80. \ INTERUPT HANDLER DEMO
  81. 0 1 IN/OUT
  82. : KEYS? ( like key?, but returns keystroke if there is one)
  83.        KEY? IF KEY ELSE 0 THEN  
  84.        BREAK-SET @ IF BREAK-SET OFF Ctrl-Break THROW THEN ;
  85.  
  86. DECIMAL
  87. : DO/ / ;    \ We do the divide within a CATCH specific to the
  88.                 \ single divide instance, for fine grain control
  89.                 \ Contrast this with catching Control-C
  90.  
  91. : RUNLOOP \ Normal return when key is hit
  92.     BEGIN  10 0 DO
  93.         10000 I ['] DO/ CATCH CASE
  94.                       0 OF   .  ( good case ) ENDOF
  95.                       0Divide OF ." DIVIDE BY ZERO" 2DROP ENDOF
  96.                        THROW ( we won't handle it here ) ENDCASE
  97.         LOOP
  98.         CR KEYS? UNTIL 
  99. ;
  100.  
  101.  
  102. 2VARIABLE /0-SAVE  ( we will want to save the vectors )
  103. 2VARIABLE CB-SAVE
  104.  
  105. : MAIN  
  106.     /0-INT get-handler /0-SAVE 2!        \ get and save old handlers
  107.     CB-INT get-handler CB-SAVE 2!
  108.     ?CS: CC-ENTRY CC-INT set-handler    \ set handlers to us
  109.     ?CS: CB-ENTRY CB-INT set-handler
  110.     ?CS: /0-ENTRY /0-INT set-handler
  111.     ." TYPE ANY KEY TO STOP, OR TYPE CONTROL-C OR BREAK" CR
  112.         BEGIN
  113.         ['] RUNLOOP CATCH ?DUP WHILE
  114.                 CASE  Ctrl-C OF ." CONTROL-C HIT!" CR ENDOF
  115.                       Ctrl-Break OF CR ." BREAK HIT!" CR ENDOF
  116.               THROW \ In this example, there is nothing to catch it
  117.         ENDCASE
  118.         REPEAT
  119.     /0-SAVE 2@ /0-INT set-handler        \ restore handlers
  120.     ( We dont need to restore the control-C handler )
  121.     CB-SAVE 2@ CB-INT set-handler ;
  122.  
  123.  
  124. INCLUDE FORTHLIB
  125. END
  126.